//
//  AMNetworkMngTool.swift
//  Amicool
//
//  Created by Mac  on 2017/6/5.
//  Copyright © 2017年 Mac . All rights reserved.
//

import Foundation


//API
let AM_LOGIN = "http://amicool.neusoft.edu.cn/api.php/login"
let AM_LIST = "http://amicool.neusoft.edu.cn/api.php/lists"
let AM_LOGINOUT = "http://amicool.neusoft.edu.cn/api.php/logout"
typealias NetworkBlock = (_ flag:String)->Void
typealias NetworkListBlock = (_ list:NSArray?)->Void
typealias NetiworkArticleListBlock = (_ article:NSArray?)->Void
class  AMNetworkMngTool:NSObject{
    static  var shared = AMNetworkMngTool() //单例
    static var  loginReturnModel:LoginReturnJson?=LoginReturnJson()
    
    //登录请求
    func  AMNetwork_Login(_ parameters:NSDictionary,block:NetworkBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in
            parameters{
                let str = "\(key)=\(value)"
                paraArray.add(str)
        }
        
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LOGIN)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回的数据进行处理
            if data != nil {
                if let json = try?JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    AMNetworkMngTool.loginReturnModel = LoginReturnJson.LoginReturnJsonWithDict(json as? NSDictionary)
                    if AMNetworkMngTool.loginReturnModel?.error == nil {
                        //异步处理
                       block!("1")
                }
                    else {
                        block!("2")
                    }
                }
            }
            
        })
        dataTask.resume()//启动任务
    }
    //视频请求
    func  AMNetwork_GetList(_ parameters:NSDictionary,block:NetworkListBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in
            parameters{
                let str = "\(key)=\(value)"
                paraArray.add(str)
        }
        
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LIST)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回的数据进行处理
            if data != nil {
                if let json = try?JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    //将返回的数据封装成模型对象
//                    var videoList = [AnyObject]()
//                    for video in (json as! NSArray){
//                        let model = VideoReturnJson.VideoReturnJsonWithDict(video as? NSDictionary)
//                        videoList.append(model!)
//                    }
                    //把视频列表返回给调用者
                   block!(json as? NSArray)
                    }
                else {
                    block!(nil)
                }
            }
                else {
                    block!(nil)
                }
        })
        dataTask.resume()//启动任务
    }
    
    //课件请求
//    func  AMNetwork_GetCourseList(_ patameters:NSDictionary,block:)
    //文章请求
    func  AMNetwork_GetArticleList(_ parameters:NSDictionary,block:NetiworkArticleListBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in
            parameters{
                let str = "\(key)=\(value)"
                paraArray.add(str)
        }
        
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LIST)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回的数据进行处理
            if data != nil {
                if let json = try?JSONSerialization.jsonObject(with: data!, options: .mutableContainers) {
                    //将返回的数据封装成模型对象
                   var articleList = [AnyObject]()
                    for article in (json as! NSArray){
                        let model = ArticleReturnJson.ArticleReturnJsonWithDict(article as? NSDictionary)
                        articleList.append(model!)
                    }
                    block!(articleList as NSArray)
////                    var videoList = [AnyObject]()
////                    for video in (json as! NSArray){
////                        let model = VideoReturnJson.VideoReturnJsonWithDict(video as? NSDictionary)
////
////                        videoList.append(model!)
////                    }
//                    //把文章列表返回给调用者
//                    block!(videoList as NSArray)
                }
                else {
                    block!(nil)
                }
            }
            else {
                block!(nil)
            }
        })
        dataTask.resume()//启动任务
    }

    func AMNetwork_Loginout(_ parameters:NSDictionary,block:NetworkBlock?){
        let paraArray = NSMutableArray()
        for (key,value) in parameters{
            let str = "\(key)=\(value)"
            paraArray.add(str)
        }
        let body = paraArray.componentsJoined(by: "&")
        let url = URL(string: AM_LOGINOUT)
        var request = URLRequest(url: url!)
        request.httpMethod = "post"
        request.httpBody = body.data(using: String.Encoding.utf8)
        let dataTask = URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) in
            //对返回数据进行处理
            if data != nil{ //返回的数据不是nil
                let json = try? JSONSerialization.jsonObject(with: data!, options: .mutableContainers)
                if json != nil{ //解析出来的数据不是nil 说明 是存在error
                    block!("0")
                }
                if json == nil{ //说明不是标准json 解析失败
                    block!("1")     //退出成功
                }
                
            }
            if data == nil{ //网络错误
                block!("2")
            }
        })
        dataTask.resume() //启动任务
        
    }
}
